package com.example.gkcircle.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
 * Created by Jehu on 16/3/24.
 */
public class DateUtil {

    public static String getLastMinTime(long lastTime) {
        String pattern = null;
        Long when = lastTime;
        long now = System.currentTimeMillis();
        if (now - when <= 1000 * 30 * 1) {
            return "刚刚";
        } else if (now - when <= 1000 * 60 * 1) {
            return "1分钟前";
        } else if (now - when <= 1000 * 60 * 2) {
            return "2分钟前";
        } else if (now - when <= 1000 * 60 * 3) {
            return "3分钟前";
        } else if (now - when <= 1000 * 60 * 5) {
            return "5分钟前";
        } else if (now - when <= 1000 * 60 * 10) {
            return "10分钟前";
        } else if (now - when <= 1000 * 60 * 15) {
            return "15分钟前";
        } else if (now - when <= 1000 * 60 * 20) {
            return "20分钟前";
        } else if (now - when <= 1000 * 60 * 25) {
            return "25分钟前";
        } else if (now - when <= 1000 * 60 * 30) {
            return "30分钟前";
        } else if (now - when <= 1000 * 60 * 60 * 1) {
            return "1小时前";
        } else if (now - when <= 1000 * 60 * 60 * 2) {
            return "2小时前";
        } else if (getCalender(now, when, 0)) {
            pattern = "今天 HH:mm";
        } else if (getCalender(now, when, 1)) {
            pattern = "昨天 HH:mm";
        } else if (getCalender(now, when, 2)) {
            pattern = "前天 HH:mm";
        } else {
            pattern = "MM-dd HH:mm";
        }
        Date date = new Date(when);
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        return dateFormat.format(date);
    }

    public static String getLastTime(String lastTime) {
        String pattern = null;
        Long when = filerDate(lastTime);
        long now = System.currentTimeMillis();
        if (now - when <= 1000 * 60 * 10) {
            return "刚刚";
        } else if (now - when <= 1000 * 60 * 30) {
            return "30分钟前";
        } else if (now - when <= 1000 * 60 * 60 * 1) {
            return "1小时前";
        } else if (now - when <= 1000 * 60 * 60 * 2) {
            return "2小时前";
        } else if (now - when <= 1000 * 60 * 60 * 3) {
            return "3小时前";
        } else if (now - when <= 1000 * 60 * 60 * 4) {
            return "4小时前";
        } else if (now - when <= 1000 * 60 * 60 * 5) {
            return "5小时前";
        } else if (now - when <= 1000 * 60 * 60 * 6) {
            return "6小时前";
        } else if (getCalender(now, when, 0)) {
            pattern = "今天 HH:mm";
        } else if (getCalender(now, when, 1)) {
            pattern = "昨天 HH:mm";
        } else if (getCalender(now, when, 2)) {
            pattern = "前天 HH:mm";
        } else {
            pattern = "MM-dd HH:mm";
        }
        Date date = new Date(when);
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        return dateFormat.format(date);
    }

    public static long filerDate(String date) {
        Date d = null;
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            d = format.parse(date);
        } catch (Exception e) {
            // TODO: handle exception
        }
        return d.getTime();
    }

    private static boolean getCalender(long now, long when, int num) {
        Date nowDate = new Date(now);
        Date whenDate = new Date(when);
        Calendar nowCalendar = Calendar.getInstance();
        Calendar whenCalendar = Calendar.getInstance();
        nowCalendar.setTime(nowDate);
        whenCalendar.setTime(whenDate);
        return nowCalendar.get(Calendar.YEAR) == whenCalendar.get(Calendar.YEAR) && (nowCalendar.get(Calendar.DAY_OF_YEAR) - num) == whenCalendar.get(Calendar.DAY_OF_YEAR);
    }


    /**
     * 获取系统年月日时分秒
     */
    public static String getDates() {
        // TODO Auto-generated method stub
        String upDate;
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm:ss");
        upDate = sdf.format(date);
        return upDate;
    }

}
